<?php

define('table','article_class');

class article_categoryControlPage extends commonControlPage {

    public function categoryModule() {
        $category = new class_category();
        
        $this->view->lists = $category->doList(table);
        $act = $_REQUEST['act'] ? $_REQUEST['act'] : 'list';
        call_user_func(array($this,$act.'Action'));
    }

    private function listAction() {
        $this->view->headlink = '<a href="?app='.APP.'&mod='.MOD.'&act=add">添加</a>';
        $this->showMainPage('admincp/'.table.'_list');
    }


	public function deleteModule() {
        if(AJAX) {
            $iid = (int)$_GET['iid'];
            $_ENV['db']->runSql("DELETE FROM ".table." WHERE `class_id` = '{$iid}'");
            $this->outputJson(1,'操作成功');
        } else {
            $this->showMessage('Bad request');
        }
    }

    private function modiAction() {
        $class_id = $_GET['class_id'];

        $class = $_ENV['db']->fetch("SELECT * FROM ".table." WHERE `class_id` = '{$class_id}'");
        
        if (!$class) $this->showMessage('指定的分类不存在',AJAX ? '0' : 'back');

        if (POST && AJAX) {
            $columns = array('class_name','parent_id','sort');
            $data = getRequests($columns);

            $data['parent_id'] = $data['parent_id'] ? $data['parent_id'] : '0';

            if (!$data['class_name']) {
                $this->outputJson(0,'请填写分类名称');
            }

            if (!preg_match('/[0-9]+/',$data['parent_id']) || ($data['parent_id'] && !$_ENV['db']->fetchCol("SELECT class_id FROM ".table." WHERE class_id = '{$data['parent_id']}'"))) {
                $this->outputJson(0,'指定的上级分类不存在');
            }

            if (!preg_match('/[0-9]+/',$data['sort'])) {
                $this->outputJson(0,'排序必须为数字');
            }

            $category = new class_category();
            
            if ($category->modi(table,$class_id,$data['class_name'],$data['parent_id'],$data['sort'])) {
                class_cache::delete('article_class');
                $this->outputJson(1,'操作成功');
            } else {
                $this->outputJson(0,'操作失败，请检查输入');
            }
        }

        $this->view->class = $class;
        $this->showMainPage('admincp/'.table.'_modi');
    }

    private function addAction() {
        if (POST && AJAX) {
            $columns = array('class_name','parent_id','sort');
            $data = getRequests($columns);

            $data['parent_id'] = $data['parent_id'] ? $data['parent_id'] : '0';

            if (!$data['class_name']) {
                $this->outputJson(0,'请填写分类名称');
            }

            if (!preg_match('/[0-9]+/',$data['parent_id']) || ($data['parent_id'] && !$_ENV['db']->fetchCol("SELECT class_id FROM ".table." WHERE class_id = '{$data['parent_id']}'"))) {
                $this->outputJson(0,'指定的上级分类不存在');
            }

            if (!preg_match('/[0-9]+/',$data['sort'])) {
                $this->outputJson(0,'排序必须为数字');
            }

            $category = new class_category();
            
            if ($category->add(table,$data['class_name'],$data['parent_id'],$data['sort'])) {                
                class_cache::delete('article_class');
                $this->outputJson(1,'操作成功');
            } else {
                $this->outputJson(0,'操作失败，请检查输入');
            }
        }
        $this->showMainPage('admincp/'.table.'_modi');
    }
}